package com.asiainfo.ssm.execute.dao.impl;

import java.rmi.RemoteException;

import org.apache.commons.lang.math.NumberUtils;

import com.asiainfo.commons.util.DateTimeUtil;
import com.asiainfo.ssm.execute.bo.BOTiOSmsBatchEngine;
import com.asiainfo.ssm.execute.bo.BOTiOSmsImmeEngine;
import com.asiainfo.ssm.execute.dao.interfaces.ITiSmsDAO;
import com.asiainfo.ssm.execute.ivalues.IBOTiOSmsBatchValue;
import com.asiainfo.ssm.execute.ivalues.IBOTiOSmsImmeValue;

public class TiSmsDAOImpl implements ITiSmsDAO {
	@Override
	public void saveTiOSmsImme(IBOTiOSmsImmeValue value) throws Exception, RemoteException {

		if (value.isNew()) {
			long newId = BOTiOSmsImmeEngine.getNewId().longValue();

			String idStr = DateTimeUtil.getTodayYYMMDD() + newId;
			value.setSmsNoticeId(NumberUtils.toLong(idStr, newId));
			value.setPartitionId((int) (newId % 1000));
		}

		BOTiOSmsImmeEngine.save(value);
	}

	@Override
	public void saveTiOSmsBatch(IBOTiOSmsBatchValue value) throws Exception, RemoteException {
		if (value.isNew()) { 
			long newId = BOTiOSmsBatchEngine.getNewId().longValue(); 
			String idStr = DateTimeUtil.getTodayYYMMDD() + newId;
			value.setSmsNoticeId(NumberUtils.toLong(idStr, newId));
			value.setPartitionId((int) (newId % 1000)); 
		}

		BOTiOSmsBatchEngine.save(value);
	}

	@Override
	public void saveTiOSmsImme(IBOTiOSmsImmeValue[] values) throws Exception, RemoteException {
		for (IBOTiOSmsImmeValue value : values) {
			if (value.isNew()) {
				long newId = BOTiOSmsImmeEngine.getNewId().longValue();
				value.setSmsNoticeId(newId);
				value.setPartitionId((int) (newId % 1000));
			}
		}
		BOTiOSmsImmeEngine.saveBatch(values);
	}

	@Override
	public void saveTiOSmsBatch(IBOTiOSmsBatchValue[] values) throws Exception, RemoteException {
		for (IBOTiOSmsBatchValue value : values) {
			if (value.isNew()) {
				long newId = BOTiOSmsBatchEngine.getNewId().longValue();
				value.setSmsNoticeId(newId);
				value.setPartitionId((int) (newId % 1000));
			}
		}
		BOTiOSmsBatchEngine.saveBatch(values);
	}

}
